<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 靠右的按钮 -->
      <page-tools>
        <template v-slot:after>
          <el-button type="primary" size="small" @click="addPerms(1,'0')">添加权限</el-button>
        </template>
      </page-tools>
      <!-- 表格 -->
      <el-table border :data="permissionliist" row-key="id">
        <el-table-column label="权限名称" prop="name" />
        <el-table-column label="权限标记" prop="code" />
        <el-table-column label="权限描述" prop="description" />
        <el-table-column align="center" label="操作">
          <template #default="{row}">
            <el-button type="text" size="small" @click="addPerms(2,row.id)">新增子权限</el-button>
            <el-button type="text" size="small" @click="editPerm(row.id)">编辑权限</el-button>
            <el-button type="text" size="small" @click="delPermission(row.id)">删除权限</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-dialog :title="formData.id?'编辑子权限':'新增子权限'" :visible="showDialog" @close="btnCancel">
        <!-- 表单 -->
        <el-form ref="perForm" :model="formData" :rules="rules" label-width="120px">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="formData.name" style="width:90%" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="formData.code" style="width:90%" />
          </el-form-item>
          <el-form-item label="权限描述" prop="description">
            <el-input v-model="formData.description" style="width:90%" />
          </el-form-item>
          <el-form-item label="开启">
            <el-switch
              v-model="formData.enVisible"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" type="primary" @click="btnOK">确定</el-button>
            <el-button size="small" @click="btnCancel">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { addPerm, delPermission, getPermissionDetail, getPermissionList, updataPerm } from '@/api/permission'
import { listToTreeData } from '@/utils'
export default {
  data() {
    return {
      formData: {
        name: '',
        code: '',
        description: '',
        enVisible: '1'
      },
      showDialog: false,
      permissionliist: [],
      rules: {
        name: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    addPerms(type, id) {
      this.showDialog = true
      this.formData.type = type
      this.formData.pid = id
    },
    async  btnOK() {
      if (this.formData.id) {
        await this.$refs.perForm.validate()
        await updataPerm(this.formData)
        this.$message.success('编辑OK')
        this.showDialog = false
        this.getPermissionList()
      } else {
        await this.$refs.perForm.validate()
        await addPerm(this.formData)
        this.$message.success('新增OK')
        this.showDialog = false
        this.getPermissionList()
      }
    },
    btnCancel() {
      this.formData = {
        name: '',
        code: '',
        description: '',
        enVisible: '1'
      }
      this.$refs.perForm.resetFields()
      this.showDialog = false
    },
    async editPerm(id) {
      this.showDialog = true

      this.formData = await getPermissionDetail(id)
    },
    async getPermissionList() {
      this.permissionliist = listToTreeData(await getPermissionList(), '0')
      console.log(this.permissionliist)
    },
    async delPermission(id) {
      await this.$confirm('是否删除？')
      await delPermission(id)
      this.$message.success('删除OK')
      this.getPermissionList()
    }
  }
}
</script>
<style>

</style>
